package jerry.html.data.convertors;

import java.util.List;
import java.util.Map;

import jerry.html.data.CareerPlayer;
import jerry.html.data.DataType;
import jerry.html.data.Game;
import jerry.html.data.Player;
import jerry.html.data.Stuff;
import jerry.html.data.StuffType;
import jerry.html.data.accessors.DataAccessor;
import jerry.html.events.EvtCap;

/**
 * 
 * Interface for particular data convertor.
 * 
 * More converters can be plugged in using the properties files for the convertor
 * 
 * 
 * @author (h0t@_G0|i
 * 
 */
public abstract class DataConvertor {
	
	protected Map<String, DataType> gameData;
	protected Map<String, DataAccessor> accessors;
	protected Class<? extends EvtCap> dataType = null;
	
	public void setDataType(Class<? extends EvtCap> dataType) {
		this.dataType = dataType;
	}
	
	public abstract boolean convertData(List<? extends EvtCap> eventData);
	
	public void setGameData(Map<String, DataType> gameData) {
		this.gameData = gameData;
	}
	
	public Map<String, DataType> getGameData() {
		return gameData;
	}
	
	public void setAccessors(Map<String, DataAccessor> accessors) {
		this.accessors = accessors;
	}
	protected Game getGame() {
		Game game = (Game) gameData.get("game");
		if(game==null) {
			game = new Game();
			gameData.put("game", game);
		}
		return game;
	}
	
	protected Player getPlayer(Integer id) {
		Game game = getGame();
		Player player = null;
		for(Player[] teamList : game.getPlayersInTeam().values()) {
			player = teamList[id];
			if(player != null) {
				break;
			}
		}
		if(player==null) {
			player = new Player();
			player.setId(id);
			game.getPlayersInTeam().get(0)[id]=player;
		}
		return player;
	}
	
	protected void assignTeam(Player player, Integer teamId) {
		Game game = getGame();
		if(game.getPlayersInTeam().get(teamId) == null) {
			game.getPlayersInTeam().put(teamId, new Player[20]);
		}
		for (Player[] teamList : game.getPlayersInTeam().values()) {
			teamList[player.getId()]=null;
		}
		game.getPlayersInTeam().get(teamId)[player.getId()] = player;
	}
	
	protected CareerPlayer getCareerPlayer(String playerCode) {
		CareerPlayer careerPlayer=null;
		try {
			careerPlayer = (CareerPlayer) accessors.get("player").getData(playerCode);
		} catch (Exception e) {
			System.out.println("Creating new player..");
		}
		
		if(careerPlayer==null) {
			careerPlayer = new CareerPlayer();
			careerPlayer.setIdentity(playerCode);
		}
		return careerPlayer;
	}
	
	public Stuff getPlayerStuff(Integer id, String stuffName, StuffType stuffType) {
		Player p = getPlayer(id);
		Stuff found = null;
		for (Stuff stuff : p.getTotalStuffUsed()) {
			if(stuff.getStuffName().equals(stuffType.convert(stuffName)) && stuff.getStuffType() == stuffType) {
				found = stuff;
				break;
			}
		}
		if(found == null) {
			found = new Stuff();
			found.setStuffName(stuffType.convert(stuffName));
			found.setStuffType(stuffType);
			found.setTimesPicked(0);
			found.setTimeOfUse(0D);
			p.getTotalStuffUsed().add(found);
			
		}
		return found;
	}
	
	protected StuffType getStuffType(String itemName) {
		for (StuffType stuffType : StuffType.values()) {
			if(stuffType.getStuffType(itemName)!=null) {
				return stuffType;
			}
		}
		return StuffType.SPECIAL;
	}
}
